<?php
include_once '../includes/common.php';
$proid = daddslashes($_GET['proid']);
$qq = daddslashes($_GET['qq']);
$code = daddslashes($_GET['code']);
$email = daddslashes($qq.'@qq.com');
if (!$proid || !$qq || !$code) {
    sysmsg("未选择程序或QQ不正确或验证码不正确",2,'/?mod=getprogram',true);
}
$program = $DB->get_row("SELECT * FROM authguao_program WHERE id='$proid' limit 1");
$coderow = $DB->get_row("select * from authguao_code where code='".$code."' and hm='".$email."' limit 1");
if ($program) {
    if (!$coderow) {
        sysmsg("验证码不正确！",2,'/?mod=getprogram',true);
    } else if ($coderow['time'] < TIMESTAMP - 120 || $coderow['status'] > 0) {
        sysmsg("验证码已失效，请重新获取！",2,'/?mod=getprogram',true);
    } else {
        if ($conf['active_download'] == 1) {
            if ($program['active'] == 1) {
                sysmsg("该程序已停止运行！",2,'/?mod=getprogram',true);
            }
        } else {
            $row = $DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and qq='$qq' limit 1");
            if ($row) {
                $authcode = $row['authcode'];
                $sign = $row['sign'];
                if (!$authcode || !$sign) {
                    sysmsg("授权码或特征码不能为空",2,'/?mod=getprogram',true);
                }
                require_once(SYSTEM_ROOT.'pclzip.php');
                $file_real = substr($authcode,0,16).'.zip';
                $file = ROOT.CACHE_DIR."/{$file_real}";
                if ($_GET['my'] == 'installer') {
                    $file_path = ROOT.PACKAGE_DIR.'/'.$program['catalog'].'/release6000/';
                    $file_str = file_get_contents(ROOT.PACKAGE_DIR.'/authcode.php');
                    $file_str = str_replace('1000000001',$authcode,$file_str);
                    file_put_contents($file_path.$program['authfile'],$file_str);
                    $file_name = $program['installer_name'].'_'.rand(111111,999999).'.zip';
                } else if ($_GET['my']=='updater') {
                    $file_path = ROOT.PACKAGE_DIR.'/'.$program['catalog'].'/update6000/';
                    $file_str = file_get_contents(ROOT.PACKAGE_DIR.'/authcode.php');
                    $file_str = str_replace('1000000001',$authcode,$file_str);
                    file_put_contents($file_path.$program['authfile'],$file_str);
                    $file_name = $program['updater_name'].'_'.rand(111111,999999).'.zip';
                }
                if(file_exists($file))unlink($file);
                $zip = new PclZip($file);
                $v_list = $zip->create($file_path ,PCLZIP_OPT_REMOVE_PATH,$file_path);
                $file_size = filesize("$file");
                header("Content-Description: File Transfer");
                header("Content-Type:application/force-download");
                header("Content-Length: {$file_size}");
                header("Content-Disposition:attachment; filename={$file_name}");
                readfile("$file");
            } else {
                sysmsg("该程序不存在该QQ的授权！",2,'/?mod=getprogram',true);
            }
        }
    }
} else {
    sysmsg("该程序不存在！",2,'/?mod=getprogram',true);
}
?>